package org.rrs.duker.ui.activity;

import android.content.res.Configuration;
import android.os.Bundle;
import android.app.Fragment;
import android.os.Handler;
import android.util.Log;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

import org.rrs.duker.R;
import org.rrs.duker.api.LocalApiHelper;
import org.rrs.duker.base.BaseActivity;
import org.rrs.duker.pojo.Subscribe;
import org.rrs.duker.pojo.SummaryInfo;
import org.rrs.duker.ui.fragment.SlideMenuFragment;

import java.util.List;

/**
 * Duker的主界面
 */
public class HomeActivity extends BaseActivity {

    private final static String TAG = HomeActivity.class.getSimpleName();
    private Fragment mContent;
    private SlidingMenu menu;
    List<Subscribe>  mSubscribeList;
    LocalApiHelper mLocalApiHelper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // set the Above View
        setContentView(R.layout.activity_home);

        // create SlideMenu
        menu = new SlidingMenu(this);
        menu.setMode(SlidingMenu.LEFT);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        menu.setShadowWidthRes(R.dimen.shadow_width);
        menu.setShadowDrawable(R.drawable.shadow);
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        menu.setFadeDegree(0.35f);
        menu.attachToActivity(this,SlidingMenu.SLIDING_CONTENT);
        menu.setMenu(R.layout.slidemenu);

        // get subscribe categories

        // get subscribe list
        mLocalApiHelper = new LocalApiHelper();
        mSubscribeList = mLocalApiHelper.getSubscribeList("test");
    }

    public List<Subscribe> getSubscribeList(){
        return mSubscribeList;
    }

    public List<SummaryInfo> getSummaryInfoList(String url) {
        return mLocalApiHelper.getSummaryInfoList(url);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        getFragmentManager().putFragment(outState, "mContent", mContent);
    }

    public void switchContent(Fragment fragment){
        mContent = fragment;
        getFragmentManager()
                .beginTransaction()
                .replace(R.id.content_frame, fragment)
                .commit();
        Handler h = new Handler();
        h.postDelayed(new Runnable() {
            @Override
            public void run() {
                menu.showContent();
            }
        }, 50);

    }

    @Override
    protected void onStart() {
        super.onStart();
//        getFragmentManager()
//                .beginTransaction()
//                .replace(R.id.content_frame, mContent)
//                .commit();

        // set SlideMenu View
        getFragmentManager()
                .beginTransaction()
                .replace(R.id.slidemenu_frame, new SlideMenuFragment())
                .commit();

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.d(TAG, "configuration changed");
    }
}
